﻿using Gameplay.PVE;
using NpcFramework;
using System.Collections.Generic;
using UnityEngine;
public enum SceneObjType
{
    Dynamic = 0,
    Static = 1,
    
    BirthPoint = 2,
    NpcGroup = 3,
    BirthPointParent = 4,
    Event = 5,

    /// <summary>
    /// 高端机才静态，低端级就动态
    /// </summary>
    StaticHqOnly = 6,
}
namespace com.yoozoo.gta.Gameplay.PVE.Editor
{

    [DisallowMultipleComponent]
    public class ScceneDataBind:MonoBehaviour
    {
        public int Index = -1;
        //是否为动态加载物体； 不是动态加载物体则在场景初始化时创建，动态物体在可见区域内时创建，移开后删除
        public bool IsStatic
        {
            get { return ObjType == SceneObjType.Static; }
            
        }
        public SceneObjType ObjType = SceneObjType.Dynamic;
        [HideInInspector]
        public int GroupId;
        [HideInInspector]
        public Vector3 TriggerSize;

        [HideInInspector] public string EventName;

        [HideInInspector] public bool IsNpcEvent;
        public bool IsDeleteByView { get; set; }
        [HideInInspector]
        [SerializeField]
        private NpcBrithPointData _brithpointData;

        public bool IsNavMesh = false;
        [HideInInspector]
        public NpcBrithPointData BrithPointData
        {
            get
            {
                return _brithpointData;
            }
            set
            {
                _brithpointData = value;
            }
        }

        public float NpcBornProbability;
#if UNITY_EDITOR
        private List<Mesh> meshs = new List<Mesh>();
        private Dictionary<int,List<Mesh>> groupMesh = new Dictionary<int, List<Mesh>>();
        private List<NpcBrithPointData> groupData = new List<NpcBrithPointData>();

        private void OnDrawGizmos()
        {
            
            if (ObjType == SceneObjType.BirthPoint)
            {
                //DrawBirthPoint(_brithpointData);
                Gizmos.DrawCube(transform.position + new Vector3(0f, 0.9f, 0f), new Vector3(1f, 1.8f, 1f));
            }
            if (ObjType == SceneObjType.BirthPointParent)
            {
                DrawBirthPointParent(_brithpointData);
            }
            if (ObjType == SceneObjType.NpcGroup)
            {
                DrawNpcGroup(_brithpointData);
            }

            if (ObjType == SceneObjType.Event)
            {
                Gizmos.color = Color.cyan;
                Gizmos.DrawWireCube(transform.position + new Vector3(0f, 0.9f, 0f),TriggerSize);
                if (IsNpcEvent)
                {
                    if (_brithpointData.ObjType == SceneObjType.BirthPoint)
                    {
                        DrawBirthPoint(_brithpointData);
                    }
                    else if (_brithpointData.ObjType == SceneObjType.BirthPointParent)
                    {
                        DrawBirthPointParent(_brithpointData);
                    }
                    else if (_brithpointData.ObjType == SceneObjType.NpcGroup)
                    {
                        DrawNpcGroup(_brithpointData);
                    }
                  
                    var prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(PveResourceManager.tempPeopleRes[0] + ".prefab");
                    var skinRenderers = prefab.GetComponentsInChildren<SkinnedMeshRenderer>();
                    if (meshs.Count == 0)
                    {
                        foreach (var skinRender in skinRenderers)
                        {
                            meshs.Add(skinRender.sharedMesh);
                        }
                    }

                    foreach (var mesh in meshs)
                    {
                        Gizmos.color = new Color(0, 0, 1, 1f);
                        var rotation = Quaternion.FromToRotation(Vector3.forward, _brithpointData.TriggerForward);
                        Gizmos.DrawMesh(mesh, 0, _brithpointData.TriggerPosition, rotation * Quaternion.Euler(-90f, 0, 0f));
                    }
                }
            }
        }

        private int id = -1;
        private void DrawBirthPoint(NpcBrithPointData pointData)
        {
            if (pointData != null && (meshs.Count == 0 || (id != pointData.ModelId && !pointData.IsRandom)))
            {
                meshs.Clear();
                string path;
                if (pointData.ModelId == -1 || pointData.IsRandom)
                {
                    int index = Random.Range(0, PveResourceManager.tempPeopleRes.Length);
                    path = PveResourceManager.tempPeopleRes[index];
                }
                else
                {
                    path = PveResourceManager.tempPeopleRes[pointData.ModelId];
                    id = pointData.ModelId;
                }

                var prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(path + ".prefab");
                var skinRenderers = prefab.GetComponentsInChildren<SkinnedMeshRenderer>();
                foreach (var skinRender in skinRenderers)
                {
                    meshs.Add(skinRender.sharedMesh);
                }

            }

            if (pointData.IsDeleteByView)
            {
                Gizmos.color = new Color(1, 0, 0, 1f);
            }
            else
            {
                Gizmos.color = new Color(0, 1, 0, 1f);

            }
            foreach (var mesh in meshs)
            {
                var rotation = Quaternion.FromToRotation(Vector3.forward, pointData.Forward);
                Gizmos.DrawMesh(mesh, 0, transform.position, rotation * Quaternion.Euler(-90f, 0, 0f));
            }
        }

        private void DrawBirthPointParent(NpcBrithPointData pointData)
        {
            Gizmos.color = new Color(0, 0, 1, 1f);
            Gizmos.DrawCube(transform.position + new Vector3(0f, 0.9f, 0f), new Vector3(1f, 1.8f, 1f));

            if (pointData.ChildBirthPoint != null && pointData.ChildBirthPoint.Length > 0)
            {
                Gizmos.color = Color.magenta;
                for (int i = 0; i < transform.childCount; i++)
                {
                    var child = transform.GetChild(i);
                    Gizmos.DrawLine(transform.position + new Vector3(0f, 0.9f, 0f), child.transform.position + new Vector3(0f, 0.9f, 0f));
                }
            }
        }

        private void DrawNpcGroup(NpcBrithPointData _pointData)
        {
            groupMesh = new Dictionary<int, List<Mesh>>();
            groupData = new List<NpcBrithPointData>();
            var manager = FindObjectOfType<SceneSeparateManager>();
            var groupInfo = manager.GetNpcGroupInfo(_pointData.GroupId);
            if (groupInfo == null)
            {
                Gizmos.DrawCube(transform.position + new Vector3(0f, 0.9f, 0f), new Vector3(1f, 1.8f, 1f));
            }
            else
            {
                for (int i = 0; i < groupInfo.npcPointData.Count; i++)
                {
                    var pointData = groupInfo.npcPointData[i];
                    string path;
                    if (pointData.ModelId == -1 || pointData.IsRandom)
                    {
                        int index = Random.Range(0, PveResourceManager.tempPeopleRes.Length);
                        path = PveResourceManager.tempPeopleRes[0];
                    }
                    else
                    {
                        path = PveResourceManager.tempPeopleRes[pointData.ModelId];
                    }
                    var prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(path + ".prefab");
                    var tempMeshs = prefab.GetComponentsInChildren<SkinnedMeshRenderer>();
                    List<Mesh> meshes = new List<Mesh>();
                    foreach (var meshRender in tempMeshs)
                    {
                        meshes.Add(meshRender.sharedMesh);
                    }

                    pointData.Index = i;
                    groupMesh.Add(pointData.Index, meshes);
                    groupData.Add(pointData);
                    Gizmos.color = Color.yellow;
                    for (int j = 0; j < groupData.Count; j++)
                    {
                        var meshs = groupMesh[groupData[j].Index];
                        var data = groupData[j];  
                        var rotation = Quaternion.FromToRotation(Vector3.forward, data.Forward);
                        Vector3 rotateAfterLocoPosition = transform.rotation * data.Position ;
                        foreach (var mesh in meshs)
                        {
                            Gizmos.DrawMesh(mesh, 0, transform.position + rotateAfterLocoPosition, transform.rotation * rotation * Quaternion.Euler(-90f, 0, 0f));
                        }
                    }
                }

            }
        }
#endif
    }
}
